package com.sise.wangzhan;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;

/**
 * @Classname RibbonClient
 * @Description TODO
 * @Date 2020/3/25 16:52
 * @Created by wangzhan
 */
@SuppressWarnings("deprecation")
public class RibbonClient {
    public static void main(String[] args) throws Exception {

        // 写入服务列表
        ConfigurationManager.loadPropertiesFromResources("application.properties");
        //ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8081,localhost:8085");
        // 输出服务列表
        //System.out.println("服务列表：" + ConfigurationManager.getConfigInstance().getProperty("myclient.ribbon.listOfServers"));
        // 获取客户端（如果获取不到，可通过getNamedClient方法自动创建）
        RestClient client = (RestClient) ClientFactory.getNamedClient("myclient");
        // 创建request对象
        HttpRequest request = HttpRequest.newBuilder().uri("/person/48942").build();// 写入将要访问的接口
        // 多次访问测试
        for (int i = 0; i < 10; i++) {
            // 创建response对象
            HttpResponse response = client.executeWithLoadBalancer(request);
            // 接收请求结果
            String json = response.getEntity(String.class);
            // 打印结果
            System.out.println(json);
        }

    }
}
